import "dart:io";
import "dart:typed_data";

class Peer {
  int port;
  String host = "127.0.0.1";
  InternetAddress? address;
  Future<InternetAddress>? lookup;

  /// [host] can be String,InternetAddress,Uint8List
  Peer(var host, this.port) {
    if (host is InternetAddress) {
      address = host;
      this.host = host.address;
      return;
    }
    if (host is Uint8List) {
      address = InternetAddress.fromRawAddress(host);
      this.host = address!.address;
      return;
    }
    if (host is String) {
      this.host = host;
      address = InternetAddress.tryParse(host);
      if (address == null) {
        lookup = InternetAddress.lookup(host).then((addresses) {
          return address = addresses.first;
        });
      }
      return;
    }
    Peer(host.toString(), port);
  }
  factory Peer.parse(String url) {
    var u = url.split(":");
    return Peer(u[0].trim(), int.parse(u[1]));
  }

  @override
  String toString() => "$host:$port";

  @override
  bool operator ==(var other) {
    if (other is Peer) {
      if (other.host == host && other.port == port) {
        return true;
      }
    }
    return false;
  }

  @override
  int get hashCode => "Peer($this)".hashCode;
}
